﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AtmosCreator
{
    /// <summary>
    /// Form for "play the game"
    /// </summary>
    public partial class FrmPlay : Form
    {
        /// <summary>
        /// Form father of this
        /// </summary>
        private Form _father;
        /// <summary>
        /// Business class for control the gameplay
        /// </summary>
        private Play _playControl;      

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="father">The father form of this</param>
        /// <param name="localizer">Localizer</param>
        /// <param name="game">Game used</param>
        public FrmPlay(Form father,Localizer localizer,string game)
        {
            _father = father;
            string result = "NothingToDo";
            InitializeComponent();
            localizer.LocalizeForm(this);
            try
            {
                _playControl = new Play(game);
                MediaPlayer.stretchToFit = _playControl.StrechVideo;
                result = _playControl.StartGame();
            }
            catch (Exception)
            {

                MessageBox.Show(localizer.GetError("ConfigurationFileError"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
                      
            if (result != "NothingToDo")
            {
                    MediaPlayer.URL = result;
            }
            Timer.Enabled = true;
        }


        /// <summary>
        /// When closing we make visible the father form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPlay_FormClosed(object sender, FormClosedEventArgs e)
        {
            _father.Visible = true;
        }

        /// <summary>
        /// This method is executed every second and will update the _playControl instance
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            string result=_playControl.AdvanceSecond();
            if (result != "NothingToDo")
            {
                if (result != "GameEnded")
                {
                    MediaPlayer.URL = result;
                }
                else
                {
                    Timer.Enabled = false;
                }
            }
            lblClock.Text = _playControl.PlayTime.ToShortTimeString();
        }

        /// <summary>
        /// This method is called when the media player state change and will call the EndMedia method of the
        /// Play class
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (MediaPlayer.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                _playControl.EndMedia();
            }

        }

        /// <summary>
        /// This method is called when the pause/play button is clicked
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            if (MediaPlayer.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                MediaPlayer.Ctlcontrols.play();
            }
            else
            {
                MediaPlayer.Ctlcontrols.pause();
            }
            _playControl.Pause();
        }






  


    }
}
